home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Graphics / RainbowSystem / Developer / Demo.c next >
C/C++ Source or Header  |  1997-09-07  |  11KB  |  316 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <math.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <proto/graphics.h>
  13. #include <proto/intuition.h>
  14.  
  15. #include <RainbowSystem.h>
  16.  
  17. struct RainbowSystemBase *RainbowSystemBase;
  18.  
  19. void DrawHSBSpace_RGB(APTR, UWORD, UWORD, UWORD, UWORD );
  20.  
  21. static void ErrorMessage(STRPTR text)
  22. {
  23.     struct EasyStruct easy;
  24.  
  25.     easy.es_StructSize=sizeof(struct EasyStruct);
  26.     easy.es_Flags=0;
  27.     easy.es_Title="Rainbow Demo";
  28.     easy.es_TextFormat=text;
  29.     easy.es_GadgetFormat="Ok";
  30.  
  31.     EasyRequestArgs(NULL,&easy,NULL,NULL);
  32. }
  33.  
  34. int main(int argc, char **argv )
  35. {
  36.     LONG error_code;
  37.     char scr[256];
  38.     APTR obj;
  39.  
  40.     if (argc==2)    strcpy(scr,argv[1]);
  41.     else            strcpy(scr,"Workbench");
  42.  
  43.     if (RainbowSystemBase=(struct RainbowSystemBase *)OpenLibrary("rainbow.library",1L)) {
  44.  
  45.         if (obj=ObtainScreen(scr,&error_code)) {
  46.  
  47.             ULONG width=256,height=256;
  48.             struct Window *wnd;
  49.  
  50.             if (wnd=OpenWindowTags(NULL,WA_Left,    16,
  51.                                         WA_Top,     GetScreen(obj)->WBorTop+GetScreen(obj)->RastPort.TxHeight+1+12,
  52.                                         WA_InnerWidth,   width,
  53.                                         WA_InnerHeight,  height,
  54.                                         WA_Title,   "Demo",
  55.                                         WA_IDCMP,   IDCMP_CLOSEWINDOW,
  56.                                         WA_CustomScreen,GetScreen(obj),
  57.                                         WA_Flags,   WFLG_CLOSEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH,TAG_DONE))
  58.             {
  59.                 UWORD offx=wnd->BorderLeft,
  60.                       offy=wnd->BorderTop,
  61.                       x,y;
  62.  
  63.                 BeginDraw(obj,wnd->RPort);
  64.  
  65.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  66.  
  67.                 SetWindowTitles(wnd,"DrawLine_RGB()",NULL);
  68.  
  69.                 for (y=0;y<height;y+=16)
  70.                 {
  71.                     DrawLine_RGB(obj,offx,offy+255-y,offx+y,offy,255,0,0);
  72.                     DrawLine_RGB(obj,offx+255,offy+y,offx+255-y,offy+255,0,255,0);
  73.                 }
  74.  
  75.                 Delay(150);
  76.  
  77.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  78.  
  79.                 SetWindowTitles(wnd,"DrawCircle_RGB()",NULL);
  80.  
  81.                 for (y=0;y<4;y++)
  82.                     for (x=0;x<4;x++)
  83.                     {
  84.                         DrawCircle_RGB(obj,offx+31+64*x,offy+31+64*y,31,63+y*64,63+x*64,255);
  85.                     }
  86.  
  87.                 Delay(150);
  88.  
  89.                 SetWindowTitles(wnd,"FillCircle_RGB()",NULL);
  90.  
  91.                 for (y=0;y<4;y++)
  92.                     for (x=0;x<4;x++)   FillCircle_RGB(obj,offx+31+64*x,offy+31+64*y,31,RGB(63+y*64,63+x*64,255),RGB_RED);
  93.  
  94.                 Delay(150);
  95.  
  96.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  97.  
  98.                 SetWindowTitles(wnd,"DrawPolygon_RGB()",NULL);
  99.  
  100.                 for (y=0;y<4;y++)
  101.                     for (x=0;x<4;x++)
  102.                     {
  103.                         WORD array[8];
  104.  
  105.                         array[0]=offx+x*64+8; array[1]=offy+y*64+10;
  106.                         array[2]=offx+x*64+54; array[3]=offy+y*64+40;
  107.                         array[4]=offx+x*64+15; array[5]=offy+y*64+60;
  108.                         array[6]=array[0];     array[7]=array[1];
  109.  
  110.                         DrawPolygon_RGB(obj,4,array,255,63+x*64,63+y*64);
  111.                     }
  112.  
  113.                 Delay(150);
  114.  
  115.                 SetWindowTitles(wnd,"FillPolygon_RGB()",NULL);
  116.  
  117.                 for (y=0;y<4;y++)
  118.                     for (x=0;x<4;x++)
  119.                     {
  120.                         WORD array[6];
  121.  
  122.                         array[0]=offx+x*64+8;   array[1]=offy+y*64+10;
  123.                         array[2]=offx+x*64+54;  array[3]=offy+y*64+40;
  124.                         array[4]=offx+x*64+15;  array[5]=offy+y*64+60;
  125.  
  126.                         FillPolygon_RGB(obj,3,array,RGB(255,63+x*64,63+y*64),RGB_NONE);
  127.                     }
  128.  
  129.                 Delay(150);
  130.  
  131.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  132.  
  133.                 SetWindowTitles(wnd,"FillRectangle_RGB()",NULL);
  134.  
  135.                 for (y=0;y<256;y+=16)   FillRectangle_RGB(obj,offx+y,offy+y,255-y,255-y,RGB(255,y,y),RGB_NONE);
  136.  
  137.                 Delay(150);
  138.  
  139.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  140.  
  141.                 SetWindowTitles(wnd,"FadeHorizontal_RGB()",NULL);
  142.  
  143.                 FadeHorizontal_RGB(obj,offx,offy,     width,36,RGB_RED,     RGB_WHITE);
  144.                 FadeHorizontal_RGB(obj,offx,offy+36,  width,36,RGB_GREEN,   RGB_WHITE);
  145.                 FadeHorizontal_RGB(obj,offx,offy+2*36,width,36,RGB_BLUE,    RGB_WHITE);
  146.                 FadeHorizontal_RGB(obj,offx,offy+3*36,width,36,RGB_CYAN,    RGB_WHITE);
  147.                 FadeHorizontal_RGB(obj,offx,offy+4*36,width,36,RGB_MAGENTA, RGB_WHITE);
  148.                 FadeHorizontal_RGB(obj,offx,offy+5*36,width,36,RGB_YELLOW,  RGB_WHITE);
  149.                 FadeHorizontal_RGB(obj,offx,offy+6*36,width,40,RGB_BLACK,   RGB_WHITE);
  150.  
  151.                 Delay(150);
  152.  
  153.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  154.  
  155.                 SetWindowTitles(wnd,"FadeVertical_RGB()",NULL);
  156.  
  157.                 FadeVertical_RGB(obj,offx+0*36,offy,36,height,RGB_RED,    RGB_WHITE);
  158.                 FadeVertical_RGB(obj,offx+1*36,offy,36,height,RGB_GREEN,  RGB_WHITE);
  159.                 FadeVertical_RGB(obj,offx+2*36,offy,36,height,RGB_BLUE,   RGB_WHITE);
  160.                 FadeVertical_RGB(obj,offx+3*36,offy,36,height,RGB_CYAN,   RGB_WHITE);
  161.                 FadeVertical_RGB(obj,offx+4*36,offy,36,height,RGB_MAGENTA,RGB_WHITE);
  162.                 FadeVertical_RGB(obj,offx+5*36,offy,36,height,RGB_YELLOW, RGB_WHITE);
  163.                 FadeVertical_RGB(obj,offx+6*36,offy,40,height,RGB_BLACK,  RGB_WHITE);
  164.  
  165.                 Delay(150);
  166.  
  167.                 SetWindowTitles(wnd,"DrawHSBSpace_RGB()",NULL);
  168.  
  169.                 DrawHSBSpace_RGB(obj,offx,offy,width,height);
  170.  
  171.                 Delay(150);
  172.  
  173.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  174.  
  175.                 SetWindowTitles(wnd,"Move_RGB() - Draw_RGB()",NULL);
  176.  
  177.                 Move_RGB(obj,offx+width/2-1+(width*5/12),offy+height/2-1);
  178.  
  179.                 for (y=70;y<=2520;y+=70)
  180.                 {
  181.                     float angle=y*PI/180.0;
  182.                     UWORD x0,y0;
  183.  
  184.                     x0=offx+width/2-1+(width*5/12)*cos(angle);
  185.                     y0=offy+height/2-1-(height*5/12)*sin(angle);
  186.  
  187.                     Draw_RGB(obj,x0,y0,255,255*y/2520,0);
  188.                 }
  189.  
  190.                 Delay(150);
  191.  
  192.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  193.  
  194.                 SetWindowTitles(wnd,"Various",NULL);
  195.  
  196.                 for (y=0;y<40;y++)
  197.                 {
  198.                      UWORD x0=rand()%width,
  199.                            y0=rand()%height,
  200.                            w=rand()%(width-x0),
  201.                            h=rand()%(height-y0);
  202.  
  203.                     FillRectangle_RGB(obj,offx+x0,offy+y0,w,h,RGB(rand()&0xFF,rand()&0xFF,rand()&0xFF),RGB_NONE);
  204.                 }
  205.  
  206.                 Delay(150);
  207.  
  208.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  209.  
  210.                 for (y=0;y<40;y++)
  211.                 {
  212.                     UWORD x0=rand()%(width>>1),
  213.                           y0=rand()%(height>>1),
  214.                           r0=rand()%(width>>2);
  215.  
  216.                     FillCircle_RGB(obj,offx+(width>>2)+x0,offy+(height>>2)+y0,r0,RGB(rand()&0xFF,rand()&0xFF,rand()&0xFF),RGB_NONE);
  217.                 }
  218.  
  219.                 Delay(150);
  220.  
  221.                 FillRectangle_RGB(obj,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  222.  
  223.                 for (y=0;y<40;y++)
  224.                 {
  225.                     WORD array[6];
  226.  
  227.                     array[0]=offx+rand()%width;  array[1]=offy+rand()%height;
  228.                     array[2]=offx+rand()%width;  array[3]=offy+rand()%height;
  229.                     array[4]=offx+rand()%width;  array[5]=offy+rand()%height;
  230.  
  231.                     FillPolygon_RGB(obj,3,array,RGB(rand()&0xFF,rand()&0xFF,rand()&0xFF),RGB_NONE);
  232.                 }
  233.  
  234.                 EndDraw(obj);
  235.  
  236.                 WaitPort(wnd->UserPort);
  237.  
  238.                 CloseWindow(wnd);
  239.             }
  240.             ReleaseScreen(obj);
  241.  
  242.         } else switch(error_code) {
  243.  
  244.             case RAINBOWMANAGER_ISNT_ACTIVE:    ErrorMessage("First, you must activate the RainbowManager"); break;
  245.             case NOT_ENOUGH_MEMORY:             ErrorMessage("Not enough memeory"); break;
  246.  
  247.         }
  248.  
  249.         CloseLibrary((struct Library*)RainbowSystemBase);
  250.  
  251.     } else ErrorMessage("First, you must acivate the RainbowManager");
  252.     return(0L);
  253. }
  254.  
  255. void DrawHSBSpace_RGB(APTR obj, UWORD xstart, UWORD ystart, UWORD w, UWORD h )
  256. {
  257.     UBYTE *buffer;
  258.  
  259.     if (buffer=AllocVec(3*max(w,16)*h,MEMF_ANY)) {
  260.  
  261.         UWORD www=3*w,x,y;
  262.  
  263.         for (x=0;x<w;x++)
  264.         {
  265.             UWORD hue=(6*(x<<13))/w,f=hue&0x1FFF;
  266.             ULONG xx;
  267.  
  268.             for (xx=3*x,y=0;y<h;y++,xx+=www)
  269.             {
  270.                 UBYTE v=(y<<8)/h,s=255-((v*v)>>8);
  271.                 UBYTE r,g,b;
  272.  
  273.                 #define p1 (v*(255-s))>>8
  274.                 #define p2 (v*(255*8192-s*f))>>21
  275.                 #define p3 (v*(((255-s)<<13)+s*f))>>21
  276.  
  277.                 switch (hue>>13)
  278.                 {
  279.                     case 0:     r=v;
  280.                                 g=p3;
  281.                                 b=p1;
  282.                                 break;
  283.                     case 1:     r=p2;
  284.                                 g=v;
  285.                                 b=p1;
  286.                                 break;
  287.                     case 2:     r=p1;
  288.                                 g=v;
  289.                                 b=p3;
  290.                                 break;
  291.                     case 3:     r=p1;
  292.                                 g=p2;
  293.                                 b=v;
  294.                                 break;
  295.                     case 4:     r=p3;
  296.                                 g=p1;
  297.                                 b=v;
  298.                                 break;
  299.                     case 5:     r=v;
  300.                                 g=p1;
  301.                                 b=p2;
  302.                                 break;
  303.                 }
  304.  
  305.                 buffer[xx]  =r;
  306.                 buffer[xx+1]=g;
  307.                 buffer[xx+2]=b;
  308.             }
  309.         }
  310.  
  311.         WritePixelArray_RGB(obj,xstart,ystart,w,h,buffer);
  312.  
  313.         FreeVec(buffer);
  314.     }
  315. }
  316.